﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuickTasks.ViewModels.Data;
using System.Collections;
using Microsoft.Advertising.Mobile.UI;

namespace QuickTasks
{
    public partial class FavoriteEmails : PhoneApplicationPage
    {
        bool change = false;
        FavoritePeople objFavorite = new FavoritePeople();
        string type1 = string.Empty;
        string nav = string.Empty;
        List<string> chec = new List<string>();
        public FavoriteEmails()
        {
            InitializeComponent();
          
            

        }
      
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                if (change == false)
                {
                    IEnumerable<System.Windows.Navigation.JournalEntry> back = NavigationService.BackStack;
                    foreach (var b in back)
                    {
                        if (!b.Source.ToString().Contains("MainPage.xaml"))
                            NavigationService.RemoveBackEntry();
                        break;
                    }


                }
                else
                    NavigationService.RemoveBackEntry();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom event In FavoriteEmails.xaml page", ex);
            }
         
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
           
            try
            {
                lbxEmailIds.ItemsSource = objFavorite.LoadFavorites();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo event In FavoriteEmails.xaml page", ex);
            }
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("nav", out nav))
            {
            }
            if (NavigationContext.QueryString.TryGetValue("type", out type1))
            {

                try
                {
                    check();
                }
                catch (Exception ex)
                {
                    Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded event In FavoriteEmails.xaml page", ex);
                }
            }
        }
        public void check1()
        {
            
                      IEnumerable<DependencyObject> cboxes = GetChildsRecursive(lbxEmailIds);

                      foreach (DependencyObject obj in cboxes.OfType<CheckBox>())
                      {

                          Type type = obj.GetType();
                          if (type.Name == "CheckBox")
                          {
                              CheckBox cb = obj as CheckBox;
                              foreach(string s in chec)
                              {
                                  if(cb.Tag!=null)
                                  if (cb.Tag.ToString() == s)
                              cb.IsChecked = true;
                              }
                          }
                      }
        }
        public void check()
        {
            chec.Clear();
            if (type1 == "Rec")
            {
                if (App.TrEmailId != "")
                {
                    chec = retrive(App.TrEmailId);
                    check1();
                }
            }
            else
            {
                    if(App.TcEmailId!="")
                    {
                        chec = retrive(App.TcEmailId);
                        check1();
                    }
            }
        }

        public List<string> retrive(string s)
        {
            List<string> str = new List<string>();

            string[] Emails = s.Split(new char[] { ',', ';' });
            foreach (string str1 in Emails)
            {
                str.Add(str1);
            }
            return str;

        }

        public static IEnumerable<DependencyObject> GetChildsRecursive(DependencyObject root)
        {
            List<DependencyObject> elts = new List<DependencyObject>();
            try
            {
                elts.Add(root);

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
                {
                    elts.AddRange(GetChildsRecursive(VisualTreeHelper.GetChild(root, i)));
                }
            }
            catch (Exception ex)
            {
                throw new FormatException(ex.Message);
            }
            return elts;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                change = true;
                if (type1 == "Rec")
                {
                    App.TrEmailId = "";
                    App.EmailId = "";
                }
                else
                {
                    App.EmailId1 = "";
                    App.TcEmailId = "";
                }
                IEnumerable<DependencyObject> cboxes = GetChildsRecursive(lbxEmailIds);

                foreach (DependencyObject obj in cboxes.OfType<CheckBox>())
                {

                    Type type = obj.GetType();
                    if (type.Name == "CheckBox")
                    {
                        CheckBox cb = obj as CheckBox;
                        if (cb.IsChecked == true)
                        {
                            if (type1 == "Rec")
                            {
                                if (App.TrEmailId != "")
                                    App.TrEmailId = App.TrEmailId + ";" + cb.Tag.ToString();
                                else
                                    App.TrEmailId = cb.Tag.ToString();
                                if (App.EmailId != "")
                                    App.EmailId = App.EmailId + ";" + cb.Content.ToString();
                                else
                                    App.EmailId = cb.Content.ToString();



                            }
                            else
                            {
                                if (App.TcEmailId != "")
                                    App.TcEmailId = App.TcEmailId + ";" + cb.Tag.ToString();
                                else
                                    App.TcEmailId = cb.Tag.ToString();
                                if (App.EmailId1 != "")
                                    App.EmailId1 = App.EmailId1 + ";" + cb.Content.ToString();
                                else
                                    App.EmailId1 = cb.Content.ToString();



                            }

                            if (nav == "1")
                                NavigationService.Navigate(new Uri("/Views/Message/SendEmail.xaml?type=Email", UriKind.Relative));
                            else
                                NavigationService.Navigate(new Uri("/Views/Message/AddMessages.xaml?type=Email", UriKind.Relative));


                        }

                    }

                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in ApplicationBarIconButton_Click event In FavoriteEmails.xaml page", ex);
            }
        }

    }
}